<?php
!defined('P_W') && exit('Forbidden');
require_once M_P.'lib/base/typedatabbs.class.php';
class PW_TypeDataTag extends PW_TypeDataBBS {
	var $_db;
	function __construct() {
		global $db;
		$this->_db = $db;
	}
	function PW_TypeDataGroup() {
		$this->__construct();
	}
	
	function _getDataFromBBS($hid,$count) {
		$tags = $this->_db->get_value('SELECT tags FROM pw_house_info WHERE hid='.pwEscape($hid));
		$tags = unserialize($tags);
		if (!$tags || !is_array($tags)) return array();
		$tids = $this->_getTidsByTags($tags);
		if (!$tids || !is_array($tids)) return array();
		return $this->_getThreadsByTids($tids,$count);
	}
	
	function _getThreadsByTids($tids,$count) {
		$rs = $this->_db->query('SELECT tid,fid,author,subject,postdate FROM pw_threads WHERE tid IN('.pwImplode($tids).') ORDER BY tid DESC '.pwLimit($count));
		while ($rt = $this->_db->fetch_array($rs)) {
			$temp[] = $this->_initData($rt);
		}
		return $temp;
	}
	
	function _getTidsByTags($tags) {
		if (!$tags || !is_array($tags)) return array();
		$temp = array();
		$rs = $this->_db->query('SELECT tid FROM pw_tagdata WHERE tagid IN ('.pwImplode($tags).') ORDER BY tid DESC LIMIT 20');
		while ($rt = $this->_db->fetch_array($rs)) {
			$temp[] = $rt['tid'];
		}
		return array_unique($temp);
	}
	


}
?>